package com.jsjosem.aftersales.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.jsjosem.aftersales.R;

import java.util.List;

import cn.hutool.core.util.StrUtil;

public class ImgListViewAdapter extends BaseAdapter {

    Context context;

    List<String> imgList;

    public ImgListViewAdapter(Context context, List<String> imgList) {
        this.imgList = imgList;
        this.context = context;
    }

    @Override
    public int getCount() {
        return imgList.size();
    }

    @Override
    public String getItem(int position) {
        return imgList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ImgHolder imgHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.img_list_view, parent, false);
            imgHolder = new ImgHolder(convertView);
            convertView.setTag(imgHolder);
        } else {
            imgHolder = (ImgHolder) convertView.getTag();
        }

        String imgUrl = getItem(position);
        if (StrUtil.isNotBlank(imgUrl)) {
            Glide.with(convertView)
                    .load(imgUrl)
                    .placeholder(R.drawable.rotate_pro)
                    .error(R.drawable.load_failed_24dp)
                    .override(100, 100)
                    .into(imgHolder.imageView);
        }
        return convertView;
    }

    public static class ImgHolder {
        ImageView imageView;

        public ImgHolder(View view) {
            this.imageView = view.findViewById(R.id.imgView);
        }
    }
}
